package com.itheima.web.servlet.system;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Permission;
import com.itheima.utils.BeanUtil;
import com.itheima.web.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

    /**
     * @Description:
     * @Param:
     * @return:
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
@WebServlet("/system/permission")
public class PermissionServlet extends BaseServlet {

    /**
     * @Description:
     * @Param:[request, response]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 5;
        String permissionName = "";
        try {
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
            permissionName = request.getParameter("permissionName");
        } catch (Exception e) {
            //e.printStackTrace();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Permission> permissionList = permissionService.findAllByPermissionName(permissionName);
        PageInfo<Permission> permissionPageInfo = new PageInfo<>(permissionList);
        request.setAttribute("page", permissionPageInfo);
        request.setAttribute("searchMap", permissionName);
        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-list.jsp").forward(request, response);
    }


    /**
     * @Description:
     * @Param:[request, response]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("/WEB-INF/pages/system/permission/permission-add.jsp").forward(request, response);
    }

    /**
     * @Description:
     * @Param:[request, response]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Permission permission = BeanUtil.fillBean(request, Permission.class);
        //调用service中方法
        permissionService.save(permission);
        //跳转回页面
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=findAll");
    }


    /**
     * @Description:
     * @Param:[request, response]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Permission permission = BeanUtil.fillBean(request, Permission.class);
        permissionService.delete(permission);
        //跳转回页面
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=findAll");
    }

    /**
     * @Description:
     * @Param:[request, response]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deleteArrays = "";
        try {
            deleteArrays = request.getParameter("deleteArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> permissionIds = new ObjectMapper().readValue(deleteArrays, new TypeReference<List<String>>() {
        });
        for (String PermissionIds : permissionIds) {
            permissionService.deleteById(permissionIds);
        }
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=findAll");
    }

    /**
     * @Description:
     * @Param:[request, response]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/12/1
     */
    public void findById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission", permission);
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/permission/permission-show.jsp").forward(request, response);

        //跳转回页面
        response.sendRedirect(request.getContextPath() + "/system/permission?operation=findAll");
    }
}
